package com.changfang.gold_man.service.Imp;

import com.changfang.gold_man.entity.Goods;
import com.changfang.gold_man.mapper.GoodsMapper;
import com.changfang.gold_man.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.bind.DatatypeConverter;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
//    查找所有商品
    @Override
    public List<Goods> findAll(){
        List<Goods> goodsList = goodsMapper.findAll();
        return goodsList;
    }
//    根据用户id查找商品
    @Override
    public List<Goods> findId(String user_id){
        List<Goods> goodsList = goodsMapper.findId(user_id);
        return goodsList;
    }
    //商品id查找商品
    @Override
    public Goods findGoodsId(Integer goods_id){
        Goods goods = goodsMapper.findGoodsId(goods_id);
        return goods;
    }


    //删除货物
    @Override
    public String deleteGoods(String goods_id) {
        try {
            goodsMapper.deleteGoods(Integer.parseInt(goods_id));
            return "1";
        }catch (Exception e){
            return "0";
        }

    }
    //商品上下架
    @Override
    public String goodsDeal(String goods_id, String goods_deal) {
        try {
            goodsMapper.goodsDeal(Integer.parseInt(goods_id),goods_deal);
            return "1";
        }catch (Exception e){
            return "0";
        }
    }
    //更改拥有者ID
    @Override
    public void updateUserId(Integer goods_id,String user_id) {
        goodsMapper.updateUserId(goods_id,user_id);
    }
    //修改商品信息
    @Override
    public void update(Map<String, String> goods) {
        try {
            String goods_id = goods.get("goods_id");
            String user_id = goods.get("user_id");
            String goods_name = goods.get("goods_name");
            String goods_msg = goods.get("goods_msg");
            String goods_gold = goods.get("goods_gold");
            goodsMapper.update(Integer.parseInt(goods_id),user_id,goods_name,goods_msg,Float.parseFloat(goods_gold));
        }catch (Exception e){
            System.out.println(e);
        }
    }

    //查找商品图片base64
//    @Override
//    public String selectGoodsImg(Integer goods_id){
//        GoodsImg goodsImg = goodsMapper.findGoodsImg(goods_id);
//        byte[] goods_img = goodsImg.getGoods_img();
//        if (goods_img!=null){
//            String ImgStr = "data:image/jpg;base64,"+ DatatypeConverter.printBase64Binary(goods_img);
//            return ImgStr;
//        }else{
//            return "";
//        }
//    }








}
